package com.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class img {

    public static void main(String[] arg) throws IOException {
        point();
    }

    public static void point() throws IOException {
        File file = new File("F:/a.jpg");
        Image img = ImageIO.read(file);

        BufferedImage bi = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.drawImage(img, 0, 0, 400, 300, Color.LIGHT_GRAY, null);

        Font f = new Font("宋体", Font.BOLD, 18);
        g.setFont(f); //设置字体:字体、字号、大小
        g.setColor(Color.BLACK);
        g.drawString("你好", 2, 16);
        g.dispose();
        // 将图片保存在原目录并加上前缀
        ImageIO.write(bi, "JPEG", new FileOutputStream("F:/b.jpg"));
    }

    public static void abc() throws FileNotFoundException, IOException {
//得到图片缓冲区
        BufferedImage bi = new BufferedImage

                (150, 70, BufferedImage.TYPE_INT_RGB);//INT精确度达到一定,RGB三原色，高度70,宽度150

//得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();

        g2.fillRect(0, 0, 150, 70);//填充一个矩形 左上角坐标(0,0),宽70,高150;填充整张图片
//设置颜色
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, 150, 70);//填充整张图片(其实就是设置背景颜色)

        g2.setColor(Color.RED);
        g2.drawRect(0, 0, 150 - 1, 70 - 1); //画边框

        g2.setFont(new Font("宋体", Font.BOLD, 18)); //设置字体:字体、字号、大小
        g2.setColor(Color.BLACK);//设置背景颜色

        g2.drawString("HelloWorld", 3, 50); //向图片上写字符串
        ImageIO.write(bi, "JPEG", new FileOutputStream("F:/a.jpg"));//保存图片 JPEG表示保存格式

    }
}
